package com.qlk.vorder.controller;

import java.util.List;

import com.qlk.vorder.controller.base.JsonReader;
import com.qlk.vorder.controller.base.JsonResult;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qlk.vorder.controller.base.BaseController;
import com.qlk.vorder.domain.ReserveSession;
import com.qlk.vorder.domain.query.ReserveSessionQuery;
import com.qlk.vorder.service.ReserveSessionService;
import com.qlk.vorder.utils.exceptions.ExistedException;
import com.qlk.common.utils.page.PageUtil;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;

/**
 * ReserveSessionController ：预约订单会话记录控制器
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:00
 */
@Controller
@RequestMapping("reserveSession")
public class ReserveSessionController extends BaseController {

	@Autowired
	private ReserveSessionService reserveSessionService;

	/** 视图前缀 */
	private static final String viewPrefix = "reserveSession";

	private static final Log LOG = LogFactory.getLog(ReserveSessionController.class);

	/**
	 * 分页 查询数据
	 * 
	 * @param model
	 * @param page
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "")
	public String index(Model model, PageUtil page, ReserveSessionQuery query) {
		try {
			List<ReserveSession> dataList = reserveSessionService.queryReserveSessionListWithPage(query, page);
			model.addAttribute("dataList", dataList);// 数据集合
			model.addAttribute("query", query);// 查询参数
			model.addAttribute("page", page);// 分页
		} catch (Exception e) {
			LOG.error("reserveSession index has error.", e);
		}
		return viewPrefix + "/index";
	}

	/**
	 * 预约订单会话记录----添加跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "addForward")
	public String addForward() {
		return viewPrefix + "/add";
	}

	/**
	 * 预约订单会话记录----添加
	 * 
	 * @param reserveSession
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	public Wrapper<?> add(ReserveSession reserveSession) {
		try {
			// reserveSession.setCreateUser(getLoginUserCnName());
			if (reserveSessionService.insert(reserveSession)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
			}
		} catch (ExistedException e) {
			LOG.warn("reserveSession add fail, exist reserveSession.");
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败，已经存在");
		} catch (Exception e) {
			LOG.error("reserveSession add has error.", e);
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
		}
	}

	/**
	 * 预约订单会话记录----更新跳转
	 * 
	 * @param model
	 * @param reserveSession
	 * @return
	 */
	@RequestMapping(value = "updateForward")
	public String updateForward(Model model, ReserveSession reserveSession) {
		try {
			ReserveSession reserveSessionResult = reserveSessionService.getReserveSessionById(reserveSession.getId());
			model.addAttribute("reserveSession", reserveSessionResult);
		} catch (Exception e) {
			LOG.error("reserveSession updateForward has error.", e);
		}
		return viewPrefix + "/update";
	}

	/**
	 * 预约订单会话记录----更新
	 * 
	 * @param model
	 * @param reserveSession
	 * @return
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	public Wrapper<?> update(Model model, ReserveSession reserveSession) {
		try {
			// reserveSession.setUpdateUser(getLoginUserCnName());
			if (reserveSessionService.update(reserveSession)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "更新成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "更新失败！");
			}
		} catch (Exception e) {
			LOG.error("reserveSession update has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 预约订单会话记录----删除
	 * 
	 * @param reserveSession
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Wrapper<?> delete(ReserveSession reserveSession) {
		try {
			// reserveSession.setUpdateUser(getLoginUserCnName());
			if (reserveSessionService.delete(reserveSession)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败！");
			}
		} catch (Exception e) {
			LOG.error("reserveSession delete has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 预约订单会话记录----查询-无分页
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "query")
	@ResponseBody
	public Wrapper<?> query(ReserveSessionQuery query) {
		try {
			List<ReserveSession> list = reserveSessionService.queryReserveSessionList(query);
			if (!CollectionUtils.isEmpty(list)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询配置信息失败！");
			}
		} catch (Exception e) {
			LOG.error("reserveSession query has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 查询预约订单会话记录详情
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	@ResponseBody
	public Wrapper<?> detail(ReserveSessionQuery query) {
		if (null == query || null == query.getId()) {
			return illegalArgument();
		}

		try {
			ReserveSession reserveSession = reserveSessionService.getReserveSessionById(query.getId());
			if (reserveSession != null) {
				return new Wrapper<ReserveSession>().result(reserveSession);
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询预约订单会话记录详情失败！");
			}
		} catch (Exception e) {
			LOG.warn("detail reserveSession has error.", e);
			return error();
		}
	}


    /**
     * 查询订单回话信息
     * @param model
     * @param rows
     * @param page
     * @param reserveSessionQuery
     * @return
     */
    @RequestMapping(value = "/queryReserveSession", method = RequestMethod.POST)
    @ResponseBody
    public JsonReader queryReserveSession(Model model, Integer rows, Integer page, ReserveSessionQuery reserveSessionQuery) {
        JsonReader result = new JsonReader();
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageSize(rows);
        pageUtil.setCurPage(page);

        try {
//            List<ReserveSession> dataList = reserveSessionService.queryReserveSessionListWithPage(reserveSessionQuery, pageUtil);
            List<ReserveSession> dataList = reserveSessionService.queryReserveSessionList(reserveSessionQuery);
            if(dataList != null && dataList.size()>0){
            	for (ReserveSession reserveSession : dataList) {
    				  if(StringUtils.isNotBlank(reserveSession.getTimeLength())){
    					  //毫秒转分钟
    					  long ms =Long.parseLong(reserveSession.getTimeLength());
    					  int ss = 1000;
    						int mi = ss * 60;
    						int hh = mi * 60;
    						int dd = hh * 24;
    						long day = ms / dd;
    						long hour = (ms - day * dd) / hh;
    						long minute = (ms - day * dd - hour * hh) / mi;
    						long second = (ms - day * dd - hour * hh - minute * mi) / ss;
    						String strMinute = minute < 10 ? "0" + minute : "" + minute;
    						String strSecond = second < 10 ? "0" + second : "" + second;
    						reserveSession.setTimeLength(strMinute+"分"+strSecond+"秒");
    				  }
    			}
            }
            result.setCurrpage(pageUtil.getCurPage());
            result.setTotalpages(pageUtil.getTotalPage());
            result.setTotalrecords(pageUtil.getTotalRow());
            result.setResult(dataList);

        } catch (Exception e) {
            LOG.error("product--> queryProduct 查询数据异常", e);
            result.setCode(JsonResult.CODE_SERVER_ERROR);
        }
        return result;
    }

}
